<template>
  <div class="index_product">
    <ul class="tab">
      <li v-for="item in tab" :key="item.id">
        <a @tap="linkListPage(item)" 
        :style="{color:app_tabBar.config.secondcolor,borderColor:app_tabBar.config.secondcolor}">
          <h3>{{item.name}}</h3>
        </a>
      </li>
    </ul>
    <swiper :current="current" class="list" :style="'height:'+(imgHeights[current]+210)+'rpx'" @change="imageChange" circular="true">
      <block v-for="item in data.list" :key="item.id">
        <swiper-item class="slide-item">
          <div class="body">
            <figure @tap="linkViewPage(item)">
              <image :src="item.imgurl" mode="widthFix" @load="imageLoad" class="slide-image" :style="'height:'+imgHeights[current]+'rpx;width:690rpx;'" />
            </figure>
            <div class="content">
              <h4 @tap="linkViewPage(item)" :style="{color:app_tabBar.config.firstcolor}">{{item.title}}</h4>
              <p class="desc" :style="{color:app_tabBar.config.secondcolor}">{{item.description}}</p>
              <p class="price">{{item.price}}</p>
            </div>
          </div>
        </swiper-item>
      </block>
    </swiper>
    <div class="control-label">
      <div class="control-prev" @tap="handlePrev" :style="{color:app_tabBar.config.secondcolor,borderColor:app_tabBar.config.secondcolor}">
        <i-icon type="return" />
      </div>
      <a @tap="linkMore" class="more" 
      :style="{color:app_tabBar.config.secondcolor,borderColor:app_tabBar.config.secondcolor}">
      {{data.more_text}}</a>
      <div class="control-next" @tap="handleNext" :style="{color:app_tabBar.config.secondcolor,borderColor:app_tabBar.config.secondcolor}">
        <i-icon type="enter" />
      </div>
    </div>
  </div>
</template>
<script>
import { switchTab, navigateToViewPage, navigateToListPage } from '@/utils/index'
import { mapGetters } from 'vuex'
export default {
  props: {
    data: Array,
    tab: Array
  },
  data() {
    return {
      imgHeights: [],
      current: 0,
      ratio: Number
    }
  },
  computed: {
    ...mapGetters(['app_tabBar'])
  },
  methods: {
    imageLoad(e) {
      const imgwidth = e.mp.detail.width
      const imgheight = e.mp.detail.height
      const ratio = imgwidth / imgheight
      this.ratio = ratio
      const viewHeight = 690 / ratio
      this.imgHeights.push(viewHeight)
    },
    imageChange(e) {
      this.current = e.mp.detail.current
    },
    handlePrev() {
      const current = this.current
      this.current = current > 0 ? current - 1 : this.data.list.length - 1
    },
    handleNext() {
      const current = this.current
      this.current = current < this.data.list.length - 1 ? current + 1 : 0
    },
    linkMore() {
      switchTab('product', this)
    },
    linkViewPage(item) {
      let type=''
      if(item.module==='2'){
          type='news'
      }
      if(item.module==='1'){
          type='about'
      }
      if(item.module==='3'){
          type='product'
      }
      navigateToViewPage(type, item, this)
    },
    linkListPage(item) {
            let url=''
      if(item.module==='2'){
          url='../../pages/news/main'
      }
      if(item.module==='1'){
        url='../../pages/about/main'
      }
      if(item.module==='3'){
         url='../../pages/product/main'
      }
      wx.switchTab({
        url: '../../pages/product/main'
      })
      navigateToListPage(item, this)
    }
  }
}
</script>
<style lang="scss" scoped>
.index_product {
  .tab {
    display: flex;
    flex-direction: row;
    margin-bottom: 20px;
    overflow-y: scroll;
    width: 100%;
    li {
      flex: 1;
    }
    a {
      background: #fff;
      padding: 8px 30px;
      border-radius: 0;
      white-space: nowrap;
      border: 1px solid;
      border-color:#848484;
      color: #848484;
      font-size: 12px;
      margin-right: 10px;
    }
  }
  .list {
    width: 100%;
    .slide-item {
    }
    .content {
      text-align: left;
      border-bottom: 1px solid #999999;
      padding: 20px 20px 20px 0px;
      margin-bottom: 10px;
      h4 {
        font-size: 16px;
        color: #000;
        font-weight: 500;
      }
      .desc {
        font-size: 14px;
        color: #848484;
        height: 38px;
        overflow: hidden;
      }
    }
  }
  .control-label {
    display: flex;
    margin-top: 30px;
    justify-content: center;
    .more {
      margin: 0 15px;
    }
    .control-prev {
      padding: 2px 20px;
      border: 1px solid; 
      border-color: #dcdcdc;
      font-size: 19px;
      position: relative;
      color: #848484;

    }

    .control-next {
      padding: 2px 20px;
      border: 1px solid;
      border-color: #dcdcdc;
      font-size: 19px;
      position: relative;
      color: #848484;

    }
  }
}
</style>
